package com.acme.serverside;

import java.io.ObjectOutputStream;
import java.net.Socket;

public class ControlMessaging {

    public ControlMessaging() {
    }

    public String textMessage(String nickName, String message) {
        String result;
        switch (message) {
            case ChatServer.EXIT:
                result = String.format("%s is offline =(\n", nickName);
                break;
            case ChatServer.ONLINE:
                result = String.format("%s is online! =)\n", nickName);
                break;
            default:
                result = String.format("%s >> %s\n", nickName, message);
                break;
        }
        return result;
    }

    public boolean validateParametersSendAll(String message) {
        return !(message == null || message.isEmpty());
    }

    public boolean checkCountClient(Socket socket) throws Exception {
        if (ChatServer.sizeListClient() + 1 > 100) {
            messageOverlimit(socket);
            return false;
        }
        return true;
    }

    private void messageOverlimit(Socket socket) throws Exception {
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        Thread.sleep(100);
        out.writeObject("A client connections limit is overhead! Try again later.\n");
        out.writeObject(ChatServer.EXIT);
        out.close();
        socket.close();
    }
}